//
//  ZCGuideView.m
//  guideLogin
//
//  Created by apple on 16/3/11.
//  Copyright © 2016年 MDJ. All rights reserved.
//

#import "ZCGuideView.h"

@interface ZCGuideView ()
{
    CGFloat headViewH;
    CGFloat centerViewH;
}
@property (nonatomic,strong)UIWindow *window;
@property (nonatomic,strong)UIView *headView;
@property (nonatomic,strong)UIView *centerView;
@property (nonatomic,strong)UIView *footView;
@property (nonatomic,strong)UIImageView *titleImgView;
@property (nonatomic,strong)UIImageView *centerImgView;
@property (nonatomic,strong)UIImageView *footerImgView;
@property (nonatomic,strong)UIControl *control;

@end

@implementation ZCGuideView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.window = [UIApplication sharedApplication].keyWindow;
        [self AddcontentView];//增加View
    }
    return self;
}

-(instancetype)initWithFrame:(CGRect)frame andType:(ZCGuideViewType)type{
    self = [super initWithFrame:frame];
    if (self) {
        if (type == ZCGuideViewTypeSao) {
            
            if (IS_IPHONE_5 ||IS_IPHONE_4_OR_LESS) {
                headViewH = SCREEN_HEIGHT - 228;
                centerViewH = 175;
            }else{
                headViewH = SCREEN_HEIGHT - 242;
                centerViewH = 175;
            }
            self.centerImgView.image = [UIImage imageNamed:@"main_content"];
            self.titleImgView.image = [UIImage imageNamed:@"main_title"];
            self.footerImgView.image = [UIImage imageNamed:@"main_scan"];
            self.footerImgView.alpha = 0.5;
            self.window = [UIApplication sharedApplication].keyWindow;
            [self AddcontentView];//增加View
            self.centerView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
            
            [self.headView addSubview:self.titleImgView];
            [self.centerView addSubview:self.centerImgView];
            [self.footView addSubview:self.footerImgView];
            [self.titleImgView makeConstraints:^(MASConstraintMaker *make) {
                make.centerX.equalTo(self.headView.centerX).with.offset(0);
                make.bottom.equalTo(self.headView.bottom).with.offset(-56);
                make.height.mas_equalTo(45);
                make.width.mas_equalTo(117);
            }];
            
            [self.centerImgView makeConstraints:^(MASConstraintMaker *make) {
                make.centerX.equalTo(self.headView.centerX).with.offset(0);
                make.bottom.equalTo(self.centerView.bottom).with.offset(0);
                make.height.mas_equalTo(175);
                make.width.mas_equalTo(180);
            }];
            [self.footerImgView makeConstraints:^(MASConstraintMaker *make) {
                make.edges.equalTo(self.footView).with.insets(UIEdgeInsetsMake(0, 0, 0, 0));
            }];

        }else{
            if (IS_IPHONE_5 ||IS_IPHONE_4_OR_LESS) {
                headViewH = 399-5;
                centerViewH = 70;
            }else{
                headViewH = 399-10;
                centerViewH = 80;
            }
            self.window = [UIApplication sharedApplication].keyWindow;
            [self AddcontentView];//增加View
            [self.headView addSubview:self.titleImgView];
            [self.titleImgView makeConstraints:^(MASConstraintMaker *make) {
                make.centerX.equalTo(self.headView.centerX).with.offset(0);
                make.height.mas_equalTo(SCREEN_WIDTH/2);
                make.width.mas_equalTo(SCREEN_WIDTH);
                make.bottom.equalTo(self.headView.bottom).with.offset(0);
            }];
            _centerImgView.alpha = 0.5;
            self.centerImgView.frame = CGRectMake(0,0, SCREEN_WIDTH, centerViewH);
            [self.centerView addSubview:self.centerImgView];
            _footView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
        }
    }
    return self;
    
}

#pragma mark 添加子控件
-(void)AddcontentView{
    
    [self addSubview:self.headView];
    [self addSubview:self.centerView];
    [self addSubview:self.footView];
    
    [self.headView makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.top).with.offset(0);
        make.right.equalTo(self).with.offset(0);
        make.left.equalTo(self).with.offset(0);
        make.height.mas_equalTo(headViewH);
    }];
    [self.centerView makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.headView.bottom).with.offset(0);
        make.right.equalTo(self).with.offset(0);
        make.left.equalTo(self).with.offset(0);
        make.height.mas_equalTo(centerViewH);
    }];
    [self.footView makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.centerView.bottom).with.offset(0);
        make.right.equalTo(self).with.offset(0);
        make.left.equalTo(self).with.offset(0);
        make.bottom.equalTo(self).with.offset(0);
    }];
}

#pragma mark 显示
-(void)show{
    
    [self.window addSubview:self];
    [self.window addSubview:self.control];
    
}

#pragma mark 移除
-(void)GuideDiss{
    [self.control removeFromSuperview];
    [self removeFromSuperview];
    [self.window removeFromSuperview];
}

#pragma mark 懒加载 - 子控件

-(UIView *)headView{
    if (!_headView) {
        _headView = [[UIView alloc]init];
        _headView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
    }
    return _headView;
}

-(UIView *)centerView{
    if (!_centerView) {
        _centerView = [[UIView alloc]init];
    }
    return _centerView;
}

-(UIView *)footView{
    if (!_footView) {
        _footView = [[UIView alloc]init];
    }
    return _footView;
}

-(UIImageView *)titleImgView{
    if (!_titleImgView) {
        _titleImgView = [[UIImageView alloc]init];
        _titleImgView.image = [UIImage imageNamed:@"login_title"];
    }
    return _titleImgView;
}

-(UIImageView *)centerImgView{
    if (!_centerImgView) {
        _centerImgView = [[UIImageView alloc]init];
        _centerImgView.image = [UIImage imageNamed:@"login_center"];
    }
    return _centerImgView;
}

-(UIImageView *)footerImgView{
    if (!_footerImgView) {
        _footerImgView = [[UIImageView alloc]init];
    }
    return _footerImgView;
}

-(UIControl *)control{
    if (!_control) {
        _control = [[UIControl alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        [_control addTarget:self action:@selector(GuideDiss) forControlEvents:UIControlEventTouchUpInside];
    }
    return _control;
}

@end
